/*
 * API接口及成功处理
 */

//防止ie8报错
window.console = window.console || (function () {
        var c = {};
        c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace = c.assert = function () {
        };
        return c;
    })();
//公共方法
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery'], factory);
    } else if (typeof exports === 'object') {
        // Node, CommonJS之类的
        module.exports = factory(require('jquery'));
    } else {
        // 浏览器全局变量(root 即 window)
        var obj = factory(jQuery);
        root.Api = obj;
    }
})(this, function ($) {
    /*
     * API接口及成功处理
     */
    var Api = {
        /**
         * 登录
         */
        login: function(params, callback){
            Pub.post('/admin/login.pwd', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 导航-系统消息
         */
        accountinformation: function(params, callback){
            Pub.post('/admin/account.information', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 系统首页上半
         */
        systemhomepage: function(params, callback){
            Pub.post('/admin/system.home.page', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 登录日志
         */
        loginloglist: function(params, callback){
            Pub.post('/admin/login.log.list', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 账户设置详情信息
         */
        admindetails: function(params, callback){
            Pub.post('/admin/admin.details', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 账户设置修改密码
         */
        adminmodifypwd: function(params, callback){
            Pub.post('/admin/admin.modify.pwd', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 系统信息
         */
        steminformation: function(params, callback){
            Pub.post('/admin/stem.information', Pub.postParams(params), function(response){
                if(response.code === 0){
                    if(callback && typeof callback === 'function'){
                        callback(response.data);
                    }
                }
            })
        },
        /**
         * 上传图片限制图片大小200Kb和格式PNG/JPG/Gif
         */
        uploadLimitMore: function(elForm, elInput, type,passport,callback){
            var _this = this;
            var obj = {
                passport:passport,
                type : type
            };
            var dftime = $.cookie('lag-time');
            var sTimeUnix = new Date().getTime() + parseInt(dftime);
            var sTime = Pub.toTime(sTimeUnix);
            var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version +'&'+ Pub.token;
            var sign = md5(signTxt).toUpperCase();

            var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign='+sign;
            $(elInput).on('change', function(){
                var formData = new FormData();
                var elName=$(this).val().toLowerCase().split('.');
                if(elName[elName.length-1]=='jpg' || elName[elName.length-1]=='png' || elName[elName.length-1]=='gif'){
                    var imagSize =  $(this)[0].files[0].size;
                    if(imagSize>200*1024){
                        layui.use('layer', function(){
                            layer.msg('文件不能超过200kb', {icon: 5});
                        });
                    }else{
                        formData.append('file_data',$(this)[0].files[0]);
                        console.log(param);
                        $.ajax({
                            url: Pub.server+'/admin/img.upload'+param,
                            type: 'POST',
                            data: formData,
                            //这两个设置项必填
                            contentType: false,
                            processData: false,
                            beforeSend: function () {
                                load = layer.load(2);
                            },
                            success:function(response){
                                var res;
                                if(typeof response == 'string'){
                                    res = JSON.parse(response);
                                }else{
                                    res = response;
                                }
                                setTimeout(function(){
                                    $('.layui-layer').each(function(){
                                        if($(this).attr('type') === 'loading'){
                                            $(this).remove();
                                            $('.layui-layer-shade').remove();
                                        }
                                    });
                                },100);
                                callback(res);
                            }
                        })
                    }
                }else{
                    layui.use('layer', function(){
                        layer.msg('只能上传jpg/png/gif格式文件', {icon: 5});
                    });
                }

            });
        },
        /**
         * 上传图片限制图片大小50Kb和格式PNG/JPG
         */
        uploadLimit: function(elForm, elInput, type,passport,callback){
            var _this = this;
            var obj = {
                passport:passport,
                type : type
            };
            var dftime = $.cookie('lag-time');
            var sTimeUnix = new Date().getTime() + parseInt(dftime);
            var sTime = Pub.toTime(sTimeUnix);
            var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version +'&'+ Pub.token;

            var sign = md5(signTxt).toUpperCase();

            var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign='+sign;

            $(elInput).on('change', function(){
                var formData = new FormData();
                var elName=$(this).val().toLowerCase().split('.');
                if(elName[elName.length-1]=='jpg' || elName[elName.length-1]=='png'){
                    var imagSize =  $(this)[0].files[0].size;
                    if(imagSize>50*1024){
                        layui.use('layer', function(){
                            layer.msg('文件不能超过50kb', {icon: 5});
                        });

                    }else{
                        formData.append('file_data',$(this)[0].files[0]);

                        $.ajax({
                            url: Pub.server+'/admin/img.upload'+param,
                            type: 'POST',
                            data: formData,
                            //这两个设置项必填
                            contentType: false,
                            processData: false,
                            // beforeSend: function () {
                            //     load = layer.load(2);
                            // },
                            success:function(response){
                                var res;
                                console.log(response);
                                if(typeof response == 'string'){
                                    res = JSON.parse(response);
                                }else{
                                    res = response;
                                }
                                setTimeout(function(){
                                    $('.layui-layer').each(function(){
                                        if($(this).attr('type') === 'loading'){
                                            $(this).remove();
                                            $('.layui-layer-shade').remove();
                                        }
                                    });
                                },100);
                                callback(res);
                            }
                        })
                    }
                }else{
                    layui.use('layer', function(){
                        layer.msg('只能上传jpg/png格式文件', {icon: 5});
                    });
                }

            });
        },
        /**
         * 上传图片多图限制5张
         */
        uploadMulti5: function(elForm, elInput, type,passport,callback){
            var _this = this;
            var obj = {
                passport:passport,
                type : type
            };
            var dftime = $.cookie('lag-time');
            var sTimeUnix = new Date().getTime() + parseInt(dftime);
            var sTime = Pub.toTime(sTimeUnix);
            var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version +'&'+ Pub.token;
            var sign = md5(signTxt).toUpperCase();


            var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign='+sign;

            $(elInput).on('change', function(){
                var formData = new FormData();
                if ($(this)[0].files.length>5) {
                    layui.use('layer', function(){
                        layer.msg('一次最多传5张', {icon: 5});
                    });
                }else if($(this)[0].files.length>1){
                    for (var i = 0; i < $(this)[0].files.length; i++) {
                        formData.append('file_data[]',$(this)[0].files[i]);
                    };
                }else{
                    formData.append('file_data',$(this)[0].files[0]);
                };
                console.log($(this)[0].files);
                $.ajax({
                    url: Pub.server+'/admin/img.multi.upload'+param,
                    type: 'POST',
                    data: formData,
                    //这两个设置项必填
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        load = layer.load(2);
                    },
                    success:function(response){
                        var res;
                        console.log(response);
                        if(typeof response == 'string'){
                            res = JSON.parse(response);
                        }else{
                            res = response;
                        }
                        setTimeout(function(){
                            $('.layui-layer').each(function(){
                                if($(this).attr('type') === 'loading'){
                                    $(this).remove();
                                    $('.layui-layer-shade').remove();
                                }
                            });
                        },100);
                        callback(res);
                    },
                    error:function(response){
                        console.log(response);
                    }
                })
            });
        },
        /**
         * 上传图片多图
         */
        uploadMulti: function(elForm, elInput, type,passport,callback){
            var _this = this;
            var obj = {
                passport:passport,
                type : type
            };
            var dftime = $.cookie('lag-time');
            var sTimeUnix = new Date().getTime() + parseInt(dftime);
            var sTime = Pub.toTime(sTimeUnix);
            var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version +'&'+ Pub.token;
            var sign = md5(signTxt).toUpperCase();

            var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign='+sign;
            $(elInput).on('change', function(){
                var formData = new FormData();
                if ($(this)[0].files.length>1) {
                    for (var i = 0; i < $(this)[0].files.length; i++) {
                        formData.append('file_data[]',$(this)[0].files[i]);
                    };
                }else{
                    formData.append('file_data',$(this)[0].files[0]);
                };
                console.log($(this)[0].files);
                $.ajax({
                    url: Pub.server+'/admin/img.multi.upload'+param,
                    type: 'POST',
                    data: formData,
                    //这两个设置项必填
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        load = layer.load(2);
                    },
                    success:function(response){
                        var res;
                        console.log(response);
                        if(typeof response == 'string'){
                            res = JSON.parse(response);
                        }else{
                            res = response;
                        }
                        setTimeout(function(){
                            $('.layui-layer').each(function(){
                                if($(this).attr('type') === 'loading'){
                                    $(this).remove();
                                    $('.layui-layer-shade').remove();
                                }
                            });
                        },100);
                        callback(res);
                    },
                    error:function(response){
                        console.log(response);
                    }
                })
            });
        },

        /**
         * 上传图片
         */
        upload: function(elForm, elInput, type,passport,callback){
            var _this = this;
            var obj = {
                passport:passport,
                type : type
            };
            var dftime = $.cookie('lag-time');
            var sTimeUnix = new Date().getTime() + parseInt(dftime);
            var sTime = Pub.toTime(sTimeUnix);
            var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version +'&'+ Pub.token;
            var sign = md5(signTxt).toUpperCase();

            var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign='+sign;

            $(elInput).on('change', function(){
                var formData = new FormData();
                 console.log(formData);
                formData.append('file_data',$(this)[0].files[0]);
                console.log(param);
                $.ajax({
                    url: Pub.server+'/admin/img.upload'+param,
                    type: 'POST',
                    data: formData,
                    //这两个设置项必填
                    contentType: false,
                    processData: false,
                    // beforeSend: function () {
                    //     load = layer.load(2);
                    // },
                    success:function(response){
                        var res;
                        if(typeof response == 'string'){
                            res = JSON.parse(response);
                        }else{
                            res = response;
                        }
                        setTimeout(function(){
                            $('.layui-layer').each(function(){
                                if($(this).attr('type') === 'loading'){
                                    $(this).remove();
                                    $('.layui-layer-shade').remove();
                                }
                            });
                        },100);
                        callback(res);
                    }
                })
            });
        },
    };
    return Api;
});